package com.app.view;


import lib.common.R;
import android.R.integer;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Space;



/**
 * 绘制时间line
 * @author mtf	
 *
 */
public class TimeLineView extends View{
	
	private String TAG = TimeLineView.class.getSimpleName();
	

	public int mNum;		//圆形个数
	public float mCircleRadiusBig ;  //顶部圆形半径
	
	public float mLineHeight ;	//连接线的长度
	public float mLineWidth ; //连接线宽度
	
	public int mCircleRadiusExtendsNum;  // 顶部圆形圈数
	public float mCircleRadius ;	 //圆形半径
	
	public int ColorGroupCircle;
	public int ColorChildCircle;
	public int ColorLine;
	
	
	public int mCircleRadiusExtendsWidth = 1;
	
	

	private int POINT_START_X = 0;
	private int POINT_START_Y = 0;
	
	
//	int mCircleRadiusBigExtendsSpace = 1 ;
	
	
	private Paint paint_black = new Paint();
	private Paint paint_gray = new Paint();
	private Paint paint_orange = new Paint();
	
	private Point point_start = new Point(POINT_START_X, POINT_START_Y);
	private Point point_end = new Point();
	
	private Paint paint_line = new Paint();
	private Paint paint_circle = new Paint();
	private Paint paint_circle_header = new Paint();
	private Paint paint_circle_header_stroke = new Paint();


	
	private int width_measure ; // 共需要的宽度
	private int height_measure ;// 共需要的高度


	
	
	
	
	public TimeLineView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		
		///*  OK, 接受自定义属性
        if (null != attrs) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.timeline);

            if (ta != null) {
            	
            	
            	ColorGroupCircle = ta.getColor(R.styleable.timeline_ColorRadiusBig, getResources().getColor(android.R.color.holo_green_light));
            	ColorChildCircle = ta.getColor(R.styleable.timeline_ColorRadiusSmall, getResources().getColor(android.R.color.darker_gray));
            	ColorLine = ta.getColor(R.styleable.timeline_ColorLine, getResources().getColor(android.R.color.darker_gray));
            	
            	
            	mLineWidth = ta.getDimension(R.styleable.timeline_LineWidth, 5.0f);
            	mLineHeight = ta.getDimension(R.styleable.timeline_LineWidth, 250.0f);
            	
            	mNum = ta.getInteger(R.styleable.timeline_Num, 2);		//圆形个数
            	mCircleRadiusBig = ta.getDimension(R.styleable.timeline_RadiusBig, 80);  //顶部圆形半径
            	
            	mCircleRadiusExtendsNum = ta.getInt(R.styleable.timeline_RadiusBigExtendsStorkeNum, 10); // 顶部圆形圈数
            	mCircleRadius = ta.getDimension(R.styleable.timeline_RadiusSmall, 10);	 //圆形半径
            
            	
            	
                ta.recycle();
            }
        }
        else{
        	/*
        	mNum = 8;		//圆形个数
        	mCircleRadiusBig = 30;  //顶部圆形半径
        	mCircleRadiusExtendsNum = 5;  // 顶部圆形圈数
        	mCircleRadius = 10;	 //圆形半径
        	*/
        	
        }
         
		//*/
		
        
    	 
		
	//	paint_black.setColor( getResources().getColor(android.R.color.black));
	//	paint_gray.setColor( getResources().getColor(android.R.color.darker_gray));
	//	paint_orange.setColor( getResources().getColor(android.R.color.darker_gray));
		
		
		paint_line.setColor( ColorLine );
    	paint_line.setStrokeWidth(mLineWidth);
		
    	paint_circle_header.setColor( ColorGroupCircle );
    	paint_circle.setColor( ColorChildCircle );
    	
 
    	// 顶部外部圆形扩展
    	paint_circle_header_stroke.setStyle(Style.STROKE);
		paint_circle_header_stroke.setStrokeWidth(mCircleRadiusExtendsWidth);
		paint_circle_header_stroke.setAntiAlias(true);
		
    	
    	initSize();
    	
	}
	

	void initSize(){
		
		// 重置变量
		point_start.x = POINT_START_X;
		point_start.y = POINT_START_Y;
		
		point_end.x = point_start.x;
		
	
		int LineHeight = (int)(mLineHeight+0.5f);
		int CircleRadiusBig = (int)(mCircleRadiusBig+0.5f);
	
		for(int i = 1; i<=mNum; i++)
		{
		
			
			if(i == 1){
				
				
				// 绘制外部渐变圆圈
				int radius = CircleRadiusBig;
		        for(int j=0; j<mCircleRadiusExtendsNum; j++){
		        	paint_circle_header_stroke.setColor(ColorGroupCircle/2);
		        	radius = CircleRadiusBig + mCircleRadiusExtendsWidth*j; //顶部圆形外边框扩散的半径
		        }  
		        width_measure = point_start.x + radius * 2;		//得到最终宽度,左上角+直径+(外边框扩散有间距）直径
		        
				
				
				point_start.y += mCircleRadiusBig; //到达实心圆形底部
				point_end.y = point_start.y + LineHeight; //加上线的长度
				
			}
			else{
			
				point_start.y += mCircleRadius;
				point_end.y = point_start.y + LineHeight;
		
			}
			
			Log.e(TAG, "point_start: "+point_start.toString());
			Log.e(TAG, "point_end: "+point_end.toString());
			Log.e(TAG, "width_measure: "+ width_measure);
			Log.e(TAG, "height_measure: " + height_measure);
			
			
			point_start.x = point_end.x;
			point_start.y = point_end.y;
			
		}
		
		
		
		height_measure = point_end.y - POINT_START_Y;
	 
	}
	
	
	
	// 
	public int GetColorHeader(){
		int color = paint_circle_header.getColor();
		return color;
	}
	
	
	

	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		
 

		int LineHeight = (int)(mLineHeight+0.5f);
		int CircleRadiusBig = (int)(mCircleRadiusBig+0.5f);
	
		
		// 重置变量
		point_start.x = POINT_START_X;
		point_start.y = POINT_START_Y;
		
		
		
		int space = 0;
        for(int j=0; j<mCircleRadiusExtendsNum; j++){
        	space = mCircleRadiusExtendsWidth*j;
        }  
        
		point_start.offset(CircleRadiusBig+space, CircleRadiusBig+space);
		
		
		point_end.x = point_start.x;
		
		
		
	
		
		///*
		for(int i = 1; i<=mNum; i++)
		{
		
			
			if(i == 1){	// 绘制最顶部圆圈
				
				// 绘制内部圆形
			//x	paint_circle_header.setStyle(Style.FILL);
			//	paint_circle_header.setColor(color_circle_header); // Color.parseColor("#78a355")
				
				
				
				
				// 绘制外部渐变圆圈
		        for(int j=0; j<mCircleRadiusExtendsNum; j++){
		        	paint_circle_header_stroke.setColor(ColorGroupCircle/2);  // 设置颜色
		        	space = mCircleRadiusExtendsWidth*j;
		        	canvas.drawCircle(point_start.x, point_start.y, mCircleRadiusBig+space, paint_circle_header_stroke);
		        }  
		        
				
				// 绘制内部圆形 
		        // 就以外部圆形的宽度来定义和外部圆形之间的空隙
				canvas.drawCircle(point_start.x, point_start.y, mCircleRadiusBig-space, paint_circle_header);
				
				
				canvas.drawRect(point_start.x-mCircleRadiusBig-space, point_start.y-mCircleRadiusBig-space, 
						point_start.x+mCircleRadiusBig+space, point_start.y+mCircleRadiusBig+space, paint_circle_header_stroke);
			
				
				
				point_start.y += mCircleRadiusBig; //到达圆形底部
				point_end.y = point_start.y + LineHeight; //加上线的长度
				canvas.drawLine(point_start.x, point_start.y, point_end.x, point_end.y, paint_line);
				
			}
			else{
			
				canvas.drawCircle(point_start.x, point_start.y, mCircleRadius, paint_circle);
				point_start.y += mCircleRadius;
				point_end.y = point_start.y + LineHeight;
				canvas.drawLine(point_start.x, point_start.y, point_end.x, point_end.y, paint_line);
			//	canvas.drawCircle(point_end.x, point_end.y, mCircleRadius, paint_circle);
		
			}
			
			System.out.println("start: "+point_start.toString());
			System.out.println("end: " + point_end.toString());
			
			
			point_start.x = point_end.x;
			point_start.y = point_end.y;
			
		}
		//*/
		
		
		
		
		
	}
	
	
	
	
	
	///////////////////////////////////////////////////////////////////////////////
	
	/**
     * @see android.view.View#measure(int, int)
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureWidth(widthMeasureSpec),
                measureHeight(heightMeasureSpec));
    }
	
	
    /**
     * Determines the width of this view
     * @param measureSpec A measureSpec packed into an int
     * @return The width of the view, honoring constraints from measureSpec
     */
    private int measureWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {

        	result += getPaddingLeft() + getPaddingRight();
        	//result += point_end.x - POINT_START_X;
        	result += width_measure;
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }

        return result;
    }
	
	
    /**
     * Determines the height of this view
     * @param measureSpec A measureSpec packed into an int
     * @return The height of the view, honoring constraints from measureSpec
     */
    private int measureHeight(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
        	result += getPaddingTop() + getPaddingBottom();
        	result += height_measure;
        	
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }
        return result;
    }
    
	

	

}



